package work02.stack;

/**
 * @author sunlggggg
 * @date 2017/4/16
 */

public class Stack {
    private static final int CAPACITY = 1024;
    private static int capacity;
    private static int top = -1;
    Object[] array;

    public Stack() {
        this.capacity = CAPACITY;
        array = new Object[capacity];
    }

    private int getSize() {
        if (isEmpty()) {
            return 0;
        } else {
            return top + 1;
        }
    }

    public boolean isEmpty() {
        return (top < 0);
    }

    //Get the top element of stack
    public Object top() throws StackEmptyException {

        if (isEmpty()) {
            throw new StackEmptyException("Stack is empty");
        }
        return array[top];

    }

    public void push(Object element) throws StackFullException {
        if (getSize() == CAPACITY) {
            throw new StackFullException("Stack is full");
        }
        array[++top] = element;
    }

    public Object pop() throws StackEmptyException {
        if (isEmpty()) {
            throw new StackEmptyException("Stack is empty");
        }
        return array[top--];
    }
}